package com.caocao.interceptor;

import com.caocao.domain.AccountConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 描述:本系统的拦截器处理方法
 *
 * @Author:操文健
 * @Create Time:2017-09-10 7:46
 */
public class AccountInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(AccountInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.info("开始进入拦截器中进行相关判断....");
        // 获取系统session
        HttpSession session = httpServletRequest.getSession(true);
        // 判断用户是否登录
        if (session.getAttribute(AccountConst.SESSION_USER_LOGINGO) == null) {
            logger.info("用户未登录,需要跳转到登录界面进行登陆...[" + httpServletRequest.getContextPath() + "]");
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/statlc/login.html");
            // 设置登陆后跳转到原来的界面  需要对login后的控制器进行做相应的处理
            // response.sendRedirect("/login?next=".concat(request.getRequestURI()));
            return false;
        } else {
            logger.info("用户已登陆,进入相关URL中.....");
            // 如果登陆需要对session进行重新设置参数值
            session.setAttribute(AccountConst.SESSION_USER_LOGINGO, session.getAttribute(AccountConst.SESSION_USER_LOGINGO));
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
